/** 查询项（和后端接口的对象对应） */
class FilterValue{
    columnName : string;
	columnExpression? : string;
	dataType : string;
	operator : string;
	queryValue : string;
	droplist : boolean=false;
	checkboxes:boolean=false;
	/**
	 * 
	 * @param columnName 
	 * @param dataType string number date boolean int date-year  date-month
	 * @param operator = eq like between in llike rlike greater less greaterAndEqual lessAndEqual isNull isNotNull
	 * @param droplist true|false 是否使用下拉列表
	 * @param columnExpression 
	 * @param queryValue 
	 */
	constructor(columnName : string, dataType : string='string', operator : string='=', droplist:boolean = false, checkboxes:boolean = false, columnExpression : string="", queryValue : string=""){
		this.columnName = columnName;
		this.columnExpression = columnExpression;
		this.dataType = dataType;
		this.operator = operator;
		this.droplist = droplist;
		this.checkboxes = checkboxes;
		this.queryValue = queryValue;
	}
}

type ComponentSize = 'large' | 'medium' | 'small' | 'mini';

/** 查询组件（QueryField）使用的数据对象 */
class FilterColumn{
	filterValue:FilterValue;
	label:string;
	suffix:string;
	prefix:string;
	size:ComponentSize = 'medium' //element-plus默认为large
	width?:number;
	value?:any = '';
	value2?:any = '';
	valueArr?:Array<string> = [];
	codeItems?:[];
	nameItems?:[];
	treeList?:[];

	constructor(label:string, filterValue:FilterValue, suffix:string='', prefix:string=''){
		this.label = label;
		this.filterValue = filterValue;
		this.prefix = prefix;
		this.suffix = suffix;
	}

	/** 讲组件上设置的查询值按约定格式赋值给filterValue.queryValue */
	formatQueryValue():FilterValue{
		if(this.filterValue.operator == 'between'){
			if(this.value || this.value2){
				this.filterValue.queryValue = `${this.value}|${this.value2}`;
			}else{
				this.filterValue.queryValue = '';
			}
		}else if(this.filterValue.operator == 'in'){
			if(this.valueArr && this.valueArr.length > 0){
				this.filterValue.queryValue = this.valueArr.join('|');
			}else{
				this.filterValue.queryValue = '';
			}
		}else if(this.filterValue.dataType == 'boolean'){
			if(this.value){
				this.filterValue.queryValue = "true";
			}else {
				this.filterValue.queryValue = "false";
			}
		}
		else{
			this.filterValue.queryValue = this.value as string;
		}		
		return this.filterValue;
	}
}

/** 查询项目的集合 */
class FilterColumns{
	filterColumns:Array<FilterColumn>;

	constructor(filterColumns:Array<FilterColumn> = []){
		this.filterColumns = filterColumns;
	}

	addFilterColumn(filterColumn:FilterColumn){
		this.filterColumns.push(filterColumn);
	}
	
	/** 获取查询条件值（用于传给后端接口） */
	getQueryFilters():Array<FilterValue>{
		if(!this.filterColumns)return [];
		let queryFilters:Array<FilterValue> = [];
		for(let i=0; i<this.filterColumns.length; i++){
			let filterValue = this.filterColumns[i].formatQueryValue();
			if(filterValue.queryValue){
				queryFilters.push(filterValue);
			}
		}
		return queryFilters;
	}

	/** 清空所有的查询值 */
	reset(){
		this.filterColumns.forEach(col=>{
			col.value = undefined;
			col.value2 = undefined;
			col.filterValue.queryValue = "";
			col.valueArr = [];
		})
	}
}

export {FilterValue, FilterColumn, FilterColumns}
